//
//  YSExperienceOrderDetailFooterView.m
//  Yueshi
//
//  Created by Northwan on 15/8/5.
//  Copyright (c) 2015年 悦食中国. All rights reserved.
//

#import "YSExperienceOrderDetailFooterView.h"
#import "YSExperienceOrderDetailFooterHeaderView.h"
#import "YSOrderDetailVolumeCell.h"

@interface YSExperienceOrderDetailFooterView()<UITableViewDataSource,UITableViewDelegate>
@property (strong,nonatomic) UITableView *tableView;
@property (strong,nonatomic) NSMutableArray *itemArray;
@property (strong,nonatomic) YSExperienceOrderDetailFooterHeaderView *headerView;
@property (assign,nonatomic) CGFloat allCellHeight;
@end

@implementation YSExperienceOrderDetailFooterView

-(instancetype)init{
    self = [super init];
    if(self){
        [self setFrame:[UIScreen mainScreen].bounds];
        self.headerView = [[YSExperienceOrderDetailFooterHeaderView alloc]init];
        [self initTableView];
        _itemArray = [NSMutableArray array];
        [self setBackgroundColor:PUBLIC_CARD_BG];
    }
    return self;
}

-(void)initTableView{
    self.tableView = [[UITableView alloc]init];
    [self.tableView setFrame:CGRectMake(0, 0, self.width, self.height)];
    [self.tableView setBackgroundColor:PUBLIC_CARD_BG];
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.scrollEnabled = NO;
    [self addSubview:self.tableView];
}

-(void)setOrderWithVolume:(YSOrder *)order itemArray:(NSArray *)itemArray{

    [self.itemArray addObjectsFromArray:itemArray];
    
    [self.headerView setPrice:order.orderPrice voucherAmount:order.voucherAmount volumeCount:self.itemArray.count];
    [self.tableView setTableHeaderView:self.headerView];
    
    [self.tableView reloadData];
    
    _allCellHeight += YSOrderDetailVolumeCellHeight *self.itemArray.count;
    NSLog(@"height:%f",self.headerView.height);
    _allCellHeight += self.headerView.height;
    
    //根据所有cell的高计算出allCellHeight并设置tableView的高和本身View的高
    [self.tableView setHeight:_allCellHeight];
    [self setHeight:self.tableView.height];
}

#pragma - mark UITableViewDataSource,UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.itemArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YSOrderDetailVolumeCell *cell =  [YSOrderDetailVolumeCell cellWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setVolumeItem:self.itemArray[indexPath.row]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return YSOrderDetailVolumeCellHeight;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
